package analizator;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 *
 * @author bufu
 */
public class FileProvider {

    private BufferedReader m_reader;
    private int m_nLine, m_nColumn;
    private ArrayList<Caracter> m_list;
    private String m_strLine;
    private DataInputStream m_in;

    public FileProvider(String strFilename) {
        m_list = new ArrayList<>();
        try {
            FileInputStream fstream = new FileInputStream(strFilename);
            m_in = new DataInputStream(fstream);
            m_reader = new BufferedReader(new InputStreamReader(m_in));
            m_strLine = m_reader.readLine();
            m_nLine = 1;
            m_nColumn = 0;
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    public void putCharacter(Caracter c) {
        m_list.add(c);
    }

    public void putCharacters(ArrayList<Caracter> caracters) {
        for (int i = caracters.size() - 1; i >= 0; i--) {
            m_list.add(caracters.get(i));
        }
    }

    public boolean nextSpace() {
        Caracter car = getCharacter();
        if (car == null) {
            return false;
        }
        char c = car.value;
        putCharacter(car);
        return car.isSpace();
    }

    public boolean nextDelimitor() {
        Caracter car = getCharacter();
        char c = car.value;
        putCharacter(car);
        if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
            return true;
        } else {
            return false;
        }
    }

    public Caracter getCharacter() {
        if (m_list.size() > 0) {
            return m_list.remove(m_list.size() - 1);
        }
        if (m_strLine == null) {
            return null;
        }
        while (m_nColumn == m_strLine.length()) {
            m_nLine++;
            m_nColumn = 0;
            try {
                m_strLine = m_reader.readLine();
                return new Caracter('\n', m_nLine - 1, 0);
            } catch (Exception e) {
                e.printStackTrace(System.out);
                m_strLine = null;
                return null;
            }
        }
        return new Caracter(m_strLine.charAt(m_nColumn), m_nLine, m_nColumn++);
    }

    public void close() {
        try {
            m_in.close();
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }

    public boolean nextNumberEnd() {
        return nextNumberEnd(getCharacter());
    }

    public boolean nextNumberEnd(Caracter c) {
        Caracter n;
        if (c.value == ';' || c.value == ':' || c.value == ',' || c.isSpace()) {
            putCharacter(c);
            return true;
        }
        if (c.value == '.') {
            n = getCharacter();
            boolean result = n.value == '.';
            putCharacter(n);
            putCharacter(c);
            return result;
        }
        putCharacter(c);
        return false;
    }
}
